package cn.itsource.itsource.service.impl;

import cn.itsource.itsource.query.DepartmentQuery;
import cn.itsource.itsource.domain.Department;
import cn.itsource.itsource.mapper.DepartmentMapper;
import cn.itsource.itsource.service.IDepartmentService;
import cn.itsource.itsource.util.AjaxResult;
import cn.itsource.itsource.util.PageList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public AjaxResult save(Department department) {
        departmentMapper.save(department);
        return AjaxResult.success("添加成功");

    }

    @Override
    public AjaxResult pageList(DepartmentQuery query) {
       //使用pageHelper插件后，下面的2步分页操作，即可合并为一步操作
        //1. 先根据条件获取总条数-
        //2. 根据条件获取当前这一页的记录数
        //TODO startPage方法，一定一定一定要放在查询方法的上面。且 中间不允许出现其他的查询 ，否则失效
        PageHelper.startPage(query.getCurrPage(),query.getPageSize());
        PageInfo<Department> pageInfo = new PageInfo<>(departmentMapper.findByQuery(query));
        //getTotal():获取当前页的总数  getList():获取当前页的那几条数据的具体信息(这个是我自己的理解 先这么理解)
        PageList<Department> pageList=new PageList(pageInfo.getTotal(),pageInfo.getList());
        AjaxResult result = AjaxResult.success("操作成功");
        result.setData(pageList);

        return  result;

    }

    @Override
    public AjaxResult saveOrUpdate(Department dept) {
        //1. 先校验：1.部门名称不能为空；2.部门管理员ID必须在员工表中存在；3. 上级部门也要存在


        //2. 将数据添加
        departmentMapper.save(dept);

        return AjaxResult.success("添加成功");


    }

    @Override
    public AjaxResult treeDept() {
        AjaxResult result = AjaxResult.success("查询成功");

/*
            分析：前端级联选择器能展示的数据，它的结构是固定的：
                [
                    {label:xxx,value:xxxx,children:[
                                {label:xxx,value:xxxx,children:[

                                 ]}
                        ]}
                ]


            结论：上面的嵌套循环，相当于把一个对象循环的无数次
                    这个对象： TDepartment
         */

        //1. 先查询最顶级部门的Department对象。条件：parent_id = 0
        List<Department> topDepts=departmentMapper.findDeptByParentId(0L);

        //2.查询顶级部门的下一级部门
        for (Department topDept:topDepts) {
            queryNextDeptByParent(topDept);
//            //查询的每个顶级部门的 下一级部门
//            List<Department> childDepts=departmentMapper.findDeptByParentId(topDept.getId());
//            //将查出来的下一级部门列表进行赋值
//            topDept.setChildren(childDepts);
//
//
//            //关键关键关键！！！查完顶级后，是不是要接着查顶级的下一级？？？
//            for (Department childDept : childDepts) {
//                //查下一级的下一级部门
//                List<Department> childDeptss = departmentMapper.findDeptByParentId(childDept.getId());
//                childDept.setChildren(childDeptss);
//
//                //再接着查 下一级的下一级的  下一级
//                for (Department deptss : childDeptss) {
//                    //查下一级的下一级部门
//                    List<Department> childDeptsss = departmentMapper.findDeptByParentId(childDept.getId());
//                    deptss.setChildren(childDeptsss);
//                }
//            }

        }


        result.setData(topDepts);
        return  result;

    }

    @Override
    public AjaxResult batchDel(List<Long> ids) {

         departmentMapper.batchDel(ids);
        return AjaxResult.success("删除成功");
    }



    //将上面的不断循环查询的代码，进行抽离
    public void queryNextDeptByParent(Department dept) {

        // 查它的下一级
        List<Department> childDepts = departmentMapper.findDeptByParentId(dept.getId());
        //进行赋值
        dept.setChildren(childDepts);

        //接着是不是要查它的下一级
        if (childDepts != null && childDepts.size() > 0) {
            for (Department childDept : childDepts) {
                //查它的下一级
                queryNextDeptByParent(childDept);
            }
        }

    }


}
